// pages/follow/follow.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        lasttime:0,
        cookie:wx.getStorageSync('cookie'),
        uid:wx.getStorageSync('uid'),
        eventList:[],
        isloading:false,
        pagesize:10
    },
    // 去登录
    goLogin(){
        wx.switchTab({
          url: '/pages/myInfo/myInfo',
        })
    },
    // 获取用户动态
    async getUserEvent(){
         this.setData({
            isloading:true
          })
          wx.showLoading({
            title: '数据加载中...',
          })
        const res = await wx.p.request({
            url:`${wx.root}/event`,
            data:{
                uid:this.data.uid ,
                cookie:this.data.cookie,
                lasttime:this.data.lasttime,
                pagesize :this.data.pagesize
            }
        })
        const res2 = []
        for (let i = 0; i < res.data.event.length; i++) {
            const item = res.data.event[i];
            item.data = JSON.parse(item.json);
            // console.log(item);
            let z = {};
            z.nickname = item.user.nickname;
            z.avatarDetail = item.user.avatarDetail.identityIconUrl;
            z.msg = item.data.msg;
            if(item.data.song != undefined){
                z.songUrl = item.data.song.album.blurPicUrl;
                z.songId = item.data.song.id;
                z.songName = item.data.song.name;
                z.artistName = item.data.song.artists[0].name;    
            }
            z.forwardCount = item.forwardCount;
            z.commentCount = item.info.commentCount;
            z.likedCount = item.info.likedCount;
            z.avatarUrl = item.user.avatarUrl;
            z.pics = item.pics;
            let a = new Date(item.eventTime);
            z.time = a.toLocaleDateString().replace(/\//g, "-")+" "+ a.toTimeString().substr(0, 5);
            res2.push(z)
        }
        this.setData({
            lasttime:res.data.lasttime,
            eventList:[...this.data.eventList,...res2],
            pagesize:20,
            isloading:false
        })
        wx.hideLoading();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(wx.getStorageSync('cookie'))
        this.getUserEvent()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.isloading) return;
        this.getUserEvent()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})